package com.htg.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.GradientDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;


import java.util.List;


public class DialogTextItems {

    private Context cxt;
    private int colorPrimary;
    private float cornerRadius;


    public DialogTextItems(Context cxt) {
        this.cxt = cxt;
        this.colorPrimary = cxt.getColor(R.color.blue_0166FF);
        cornerRadius = cxt.getResources().getDimensionPixelSize(R.dimen.dp_5);
    }

    public void show(List<String> itemNames, String cancelText, DialogInterface.OnCancelListener onCancelLis, AdapterView.OnItemClickListener onItemClickListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(cxt);
        AlertDialog alertDialog = builder.create();
        alertDialog.setCanceledOnTouchOutside(false);

        View view = LayoutInflater.from(cxt).inflate(R.layout.dialog_text_items, null);

        // 创建一个 GradientDrawable 对象
        GradientDrawable drawableRoot = new GradientDrawable();
        drawableRoot.setShape(GradientDrawable.RECTANGLE);
        drawableRoot.setColor(cxt.getResources().getColor(R.color.white, cxt.getTheme())); // 设置背景颜色
        drawableRoot.setCornerRadius(cornerRadius);// 设置圆角半径
        view.setBackground(drawableRoot);

        ListView listView = view.findViewById(R.id.lv_dialog_text_items);
        ItemAdapter itemAdapter = new ItemAdapter(cxt, itemNames);
        itemAdapter.setColorPrimary(colorPrimary);
        listView.setAdapter(itemAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                alertDialog.dismiss();
                onItemClickListener.onItemClick(adapterView, view, i, l);
            }
        });
        Button btCancel = view.findViewById(R.id.bt_dialog_text_items_cancel);
        // 创建一个 GradientDrawable 对象
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setColor(colorPrimary); // 设置背景颜色
        drawable.setCornerRadius(cornerRadius); // 设置圆角半径
        //
        btCancel.setBackground(drawable);
        if (cancelText != null && !cancelText.isEmpty()) {
            btCancel.setText(cancelText);
        }
        btCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                onCancelLis.onCancel(alertDialog);
            }
        });


        alertDialog.setView(view);
        alertDialog.show();


    }

    public void setColorPrimary(int colorPrimary) {
        this.colorPrimary = colorPrimary;
    }

    public void setCornerRadius(float cornerRadius) {
        this.cornerRadius = cornerRadius;
    }


    private class ItemAdapter extends BaseAdapter {

        private Context context;
        private List<String> strings;
        private int colorPrimary;

        public ItemAdapter(Context context, List<String> strings) {
            this.context = context;
            this.strings = strings;
            colorPrimary = context.getColor(R.color.blue_0166FF);
        }

        @Override
        public int getCount() {
            return strings.size();
        }

        @Override
        public Object getItem(int i) {
            return strings.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            TextView textView = new TextView(context);

            textView.setHeight(context.getResources().getDimensionPixelSize(R.dimen.dp_30));
            textView.setGravity(Gravity.CENTER);
            textView.setText(strings.get(i));
            textView.setTextColor(context.getColor(R.color.black));

            return textView;
        }

        public void setColorPrimary(int colorPrimary) {
            this.colorPrimary = colorPrimary;
        }
    }


}
